package cn.edu.scau.cmi.lijiongtao.client;

import cn.edu.scau.cmi.lijiongtao.Factory.AutoInsurance;
import cn.edu.scau.cmi.lijiongtao.Factory.BodyInjurFactory;
import cn.edu.scau.cmi.lijiongtao.Factory.CarDamageFactory;
import cn.edu.scau.cmi.lijiongtao.Factory.CasualtiesFactory;
import cn.edu.scau.cmi.lijiongtao.Factory.InsuranceFactory;
import cn.edu.scau.cmi.lijiongtao.Factory.MultipleAccidentsFactory;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FactoryClient extends Application{
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		stage.setTitle("实验三：工厂方法");
		stage.setHeight(400);
		stage.setWidth(500);
		BorderPane borderPane = new BorderPane();
		
		TextArea description = new TextArea("");
		borderPane.setBottom(description);
		
		Image image = new Image("file:image/photo.jpg");
		ImageView imageView = new ImageView();
		imageView.setFitHeight(230);
		imageView.setFitWidth(210);
		imageView.setImage(image);
		borderPane.setLeft(imageView);
		
		VBox vBox = new VBox();
		vBox.setPadding(new Insets(15, 5, 5, 5));
		vBox.setSpacing(20);
		
		HBox hBox1 = new HBox();
		hBox1.setPadding(new Insets(15, 5, 5, 5));
		hBox1.setSpacing(10);
		Label lbName = new Label("姓名：");
		Label name = new Label("李炯涛  ");
		Label lbID = new Label("学号：");
		Label ID = new Label("201627010610");
		hBox1.getChildren().add(lbName);
		hBox1.getChildren().add(name);
		hBox1.getChildren().add(lbID);
		hBox1.getChildren().add(ID);
		vBox.getChildren().add(hBox1);
		
		HBox hBox2 = new HBox();
		hBox2.setPadding(new Insets(15, 15, 5, 5));
		hBox2.setSpacing(10);
		Label kind = new Label("请选择保险");
		ChoiceBox<String> choiceBox = new ChoiceBox<String>();
		choiceBox.setItems(FXCollections.observableArrayList(
				"驾驶员身体受伤", "汽车损坏", "人员伤亡", "多种事故"));
		choiceBox.setValue("驾驶员身体受伤");
		hBox2.getChildren().add(kind);
		hBox2.getChildren().add(choiceBox);
		vBox.getChildren().add(hBox2);
		
		Button button = new Button("显示保险信息");
		vBox.getChildren().add(button);
		
		borderPane.setCenter(vBox);
		
		Scene scene = new Scene(borderPane);
		stage.setScene(scene);
		stage.show();
		
		button.setOnAction(e -> {
			String type = (String) choiceBox.getSelectionModel().getSelectedItem();
			InsuranceFactory insuranceFactory = null;
			if(type.equals("驾驶员身体受伤")) { insuranceFactory = new BodyInjurFactory();}
			else if(type.equals("汽车损坏")) { insuranceFactory = new CarDamageFactory();}
			else if(type.equals("人员伤亡")) { insuranceFactory = new CasualtiesFactory();}
			else if(type.equals("多种事故")) { insuranceFactory = new MultipleAccidentsFactory();}
			
			AutoInsurance ai = insuranceFactory.getInsurObj();
			description.setText(ai.getInfo());
		});
	}

}
